import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/home/Home.vue'

const Login = () => import('./views/login/Login')
const Test = () => import('./views/Test')
const NotAuthorization = () => import('./views/error_page/401')
const Personal = () => import('./views/home/personal')

import CreateActivity from './views/activity/create/CreateActivity'

// 活动部分
import ActivityIndex from "./views/activity/Index_blogs/BlogIndex"
import ActivityManage from "./views/activity/manage/ActivityManage"
import ActivityList from "./views/activity/activities/ActivityList"
import ElementUI from "element-ui"
import ActivityAudit from "./views/activity/ActivityAudit"
import ActivitySignUp from "./views/activity/activities/ActivitySignup"
import ActivityInvite from "./views/activity/ActivityInvite"
import UserInfo from "./views/personalCenter/PersonalInfo"
import UserImport from "./views/personalCenter/UserImport"
import ChangePwd from "./views/personalCenter/ChangePwd"


Vue.use(Router)

const router = new Router({
    mode: 'history',
    routes: [{
        path: '/',
        component: Home,
        meta: {level: 100},
        redirect: '/activities',
        children: [{
            // 活动部分首页
            path: 'activities',
            name: 'activities',
            meta: {level: 100},
            component: ActivityIndex,
        }, {
            path: 'activities/list',
            name: "activities_list",
            meta: {level: 200},
            component: ActivityList,
        }, {
            path: 'signup/:activityId',
            name: 'activities_signup',
            meta: {level: 900},
            component: ActivitySignUp
        }, {
            path: 'activities/create',
            name: 'activities_create',
            meta: {level: 300},
            component: CreateActivity
        }, {
            path: 'activities/manage',
            name: "activities_manage",
            meta: {level: 400},
            component: ActivityManage
        }, {
            path: 'activities/audit',
            name: "activities_audit",
            meta: {level: 500},
            component: ActivityAudit
        },{
            path: 'invite',
            name: 'activity_invite',
            meta: {level: 800},
            component: ActivityInvite
        },
        {
            path: 'personal',
            name: 'personal_center',
            component: Personal,
            redirect: '/personal/userinfo',
            children: [
                {
                    path: '/personal/userinfo',
                    name: 'user_info',
                    component: UserInfo
                },
                {
                    path: '/personal/import',
                    name: 'import_user',
                    component: UserImport
                },
                {
                    path: '/personal/changepwd',
                    name: 'change_password',
                    component: ChangePwd
                }
            ]
        },]
    },
        {
            path: '/login',
            name: 'login',
            component: Login
        },
        {
            path: '/test',
            name: 'test',
            component: Test,
        },
        {
            path: '/notAuthorization',
            name: 'notAuthorization',
            component: NotAuthorization,
        },
        {
            path: '*',
            redirect: '/404',
        }, {
            path: '/404',
            component: () => import("./views/error_page/404")
        }
    ]
})

/**
 * 解决报错问题：报错显示是路由重复
 * Error: Avoided redundant navigation to current location
 */
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err);
};


/**
 *  路由守卫，登录拦截
 */
router.beforeEach((to, from, next) => {
    // to将要访问的路径
    // from 代表从那个路径跳转而来
    // next 一个函数表示放行
    // next("/login") 强制跳转
    let authPath = ["/login", "/activities", "/404"]
    if (authPath.indexOf(to.path) !== -1) return next();
    let token = window.sessionStorage.getItem("access_token");
    if (!token) {
        ElementUI.Message({
            message: "请先登录！",
            type: "warning"
        })
        return next("/login"); //如果是未登录装套转到登录页面}
    }
    next();
})
export default router;
